﻿using EagleProject.LinQDataContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace EagleProject.Controllers
{
    public class OfficerController : MyController
    {
        //
        // GET: /Officer/
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Officer/ListEvent
        public ActionResult ListEvent(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/Officer/ListEvent/");
            if (!IsOfficer()) return RedirectToAccessDenied();

            int stateId = GetIntParam("stateId");
            var data = GetEventData(currentPageIndex, NumberOfRecordPerPage, stateId);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxEventList", data);
            else
            {
                ViewBag.ListState = new SelectList(DatabaseController.GetStates(), "ID", "Name");
                return View(data);
            }
        }

        private MvcPaging.IPagedList<Eventt> GetEventData(int currentPageIndex, int numberPerPage, int stateId)
        {
            var listData = DatabaseController.GetAllEvents();

            if (stateId > 0) {
                listData = from data in listData
                           where data.LocalGovArea.StateID == stateId
                           select data;
            }

            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }

        //
        // GET: /Officer/ListAccidentRecord
        public ActionResult ListAccidentRecord(int? page)
        {
            int currentPageIndex = page.HasValue ? page.Value : 1;

            if (!IsAuthenticated()) return RedirectToLogin(@"~/Officer/ListAccidentRecord/");
            if (!IsOfficer()) return RedirectToAccessDenied();

            var data = GetAccidentRecordData(currentPageIndex, NumberOfRecordPerPage);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxAccidentRecordList", data);
            else
                return View(data);
        }

        private MvcPaging.IPagedList<AccidentRecord> GetAccidentRecordData(int currentPageIndex, int numberPerPage)
        {
            var listData = DatabaseController.GetAllAccidentRecords();
            var subData = MvcPaging.IQueryableExtensions.ToPagedList(listData, currentPageIndex, numberPerPage);

            return subData;
        }
    }
}
